!function () {
    const isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
    if (!isNode) {
        return;
    } else {
        var httpProxy = require('http-proxy');
        var http = require('http');


        var proxy = httpProxy.createProxyServer();
        proxy.on('error', function (error, req, res) {
            var json;
            console.log('proxy error', error);
            if (!res.headersSent) {
                res.writeHead(500, { 'content-type': 'application/json' });
            }

            json = { error: 'proxy_error', reason: error.message };
            res.end(JSON.stringify(json));
        });

        var server = http.createServer(function (req, res) {
            if (req.url.indexOf('/api/') > -1 || req.url.indexOf('/local/') > -1) {
                proxy.web(req, res, {
                    // target: 'http://172.16.255.154:8080/'
                    // target: 'http://192.168.1.222:9090'
                    target: process.env.proxy_host||'http://tbeta.menpuji.com'
                });
                return
            }
            proxy.web(req, res, {
                target: 'http://localhost:3001'
            });
        });
        server.listen(process.env.proxy_port||8002);
    }
} ()


